package com.cljz.makepassenger.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.base.BaseFragment;
import com.cljz.makepassenger.event.CollectEvent;
import com.cljz.makepassenger.event.RefreshEvent;
import com.cljz.makepassenger.model.net.bean.GetUserInfoBean;
import com.cljz.makepassenger.present.fragment.MainMenuP;
import com.cljz.makepassenger.ui.activity.FeedbackActivity;
import com.cljz.makepassenger.ui.activity.MainActivity;
import com.cljz.makepassenger.ui.activity.MineActivityActivity;
import com.cljz.makepassenger.ui.activity.MineCollectActivity;
import com.cljz.makepassenger.ui.activity.MineInviteActivity;
import com.cljz.makepassenger.ui.activity.MyWalletActivity;
import com.cljz.makepassenger.ui.activity.PointActivity;
import com.cljz.makepassenger.utils.L;
import com.jakewharton.rxbinding.view.RxView;

import butterknife.BindView;
import butterknife.Unbinder;
import cn.droidlover.xdroidmvp.event.BusProvider;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;


/**
 * Created by apple on 2017/4/24.
 */

public class MainMenuFragment extends BaseFragment<MainMenuP> {
    @BindView(R.id.tv_activity_num)
    TextView tvActivityNum;
    @BindView(R.id.ll_my_activity)
    LinearLayout llMyActivity;
    @BindView(R.id.tv_invite_num)
    TextView tvInviteNum;
    @BindView(R.id.ll_my_invite)
    LinearLayout llMyInvite;
    @BindView(R.id.tv_collect_num)
    TextView tvCollectNum;
    @BindView(R.id.ll_collect)
    LinearLayout llCollect;
    @BindView(R.id.ll_setting)
    LinearLayout llSetting;
    @BindView(R.id.ll_tickling)
    LinearLayout llTickling;
    Unbinder unbinder;
    @BindView(R.id.ll_wallet)
    LinearLayout llWallet;
    @BindView(R.id.ll_points)
    LinearLayout llPoints;
    Unbinder unbinder1;


    public static MainMenuFragment newInstance() {
        MainMenuFragment fragment = new MainMenuFragment();
        return fragment;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        getP().getUserInfo();
    }

    @Override
    protected void initListener() {
        super.initListener();
        RxView.clicks(llSetting)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        ((MainActivity) getActivity()).nvpMenu.setCurrentItem(1);
                    }
                });
        RxView.clicks(llMyInvite)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance())
                                .to(MineInviteActivity.class)
                                .launch();
                    }
                });
        RxView.clicks(llCollect)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance())
                                .to(MineCollectActivity.class)
                                .launch();

                    }
                });

        RxView.clicks(llWallet)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance()).to(MyWalletActivity.class).launch();
                    }
                });
        RxView.clicks(llPoints)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance()).to(PointActivity.class).launch();
                    }
                });
        RxView.clicks(llMyActivity)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance())
                                .to(MineActivityActivity.class)
                                .launch();
                    }
                });
        RxView.clicks(llTickling)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        Router.newIntent((Activity) App.getInstance())
                                .to(FeedbackActivity.class)
                                .launch();
                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_main_menu;
    }

    @Override
    public MainMenuP newP() {
        return new MainMenuP();
    }


    public void getUserInfoSucceed(GetUserInfoBean.DataBean data) {
        tvActivityNum.setText(data.getNum_activity());
        tvCollectNum.setText(data.getNum_collection());
        tvInviteNum.setText(data.getNum_invite());
    }

    public void getUserInfoFail(String error) {
    }

    @Override
    public boolean useEventBus() {
        return true;
    }

    @Override
    public void bindEvent() {
        super.bindEvent();
        BusProvider.getBus().toObservable(RefreshEvent.class)
                .compose(this.<RefreshEvent>bindToLifecycle())
                .subscribe(new Action1<RefreshEvent>() {
                    @Override
                    public void call(RefreshEvent refreshEvent) {
                        L.e("RefreshEvent");
                        getP().getUserInfo();
                    }
                });
        BusProvider.getBus().toObservable(CollectEvent.class)
                .compose(this.<CollectEvent>bindToLifecycle())
                .subscribe(new Action1<CollectEvent>() {
                    @Override
                    public void call(CollectEvent collectEvent) {
                        L.e("CollectEvent");
                        getP().getUserInfo();
                    }
                });
    }

}
